package evs.sample;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Store;

import evs.location.RESTful;

@RESTful(path="Companies")
@Entity
@Indexed
public class Company
{

    @Id
    @GeneratedValue
    private Long id;
    
    @Field(index=Index.TOKENIZED, store=Store.NO)
    private String name;
    
    @Temporal(TemporalType.DATE)
    private Date founded;
    
    @Temporal(TemporalType.DATE)
    private Date abandoned;
    
    @Field(index=Index.TOKENIZED, store=Store.YES)
    private String fieldOfActivity;
    
    @OneToMany(mappedBy="company")
    @IndexedEmbedded(depth=1)
    private List<Occupation> employments = new ArrayList<Occupation>(); 
    
    @ManyToMany( fetch = FetchType.LAZY )
    @IndexedEmbedded
    private List<Skill> requiredSkills = new ArrayList<Skill>();

    public String getName()
    {
        return name;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    public Date getFounded()
    {
        return founded;
    }

    public void setFounded( Date founded )
    {
        this.founded = founded;
    }

    public Date getAbandoned()
    {
        return abandoned;
    }

    public void setAbandoned( Date abandoned )
    {
        this.abandoned = abandoned;
    }

    public String getFieldOfActivity()
    {
        return fieldOfActivity;
    }

    public void setFieldOfActivity( String fieldOfActivity )
    {
        this.fieldOfActivity = fieldOfActivity;
    }

    public Long getId()
    {
        return id;
    }
    
    public List<Occupation> getEmployments()
    {
        return employments;
    }

    public void setEmployments( List<Occupation> employments )
    {
        this.employments = employments;
    }

    public void setRequiredSkills( List<Skill> requiredSkills )
    {
        this.requiredSkills = requiredSkills;
    }

    public List<Skill> getRequiredSkills()
    {
        return Collections.unmodifiableList( requiredSkills );
    }
    
    
    public void addRequiredSkill( Skill skill )
    {
        this.requiredSkills.add( skill );
    }
    
    public void addEmployment( Occupation e )
    {
        assert e.getCompany().getId() == this.getId();
        this.employments.add( e );
    }
}
